<template>
    <div>
        <div class="search-con search-con-top">
            <div>
                名称：
                <Input v-model="searchData.name" style="width: 200px"></Input>

                <Button @click="search" type="primary" icon="ios-search" style="margin-left: 10px;">搜索</Button>
                <Button @click="reset" type="warning" icon="ios-refresh" style="margin-left: 10px;">刷新</Button>
                <Button slot="extra" @click="edit(false, false)" type="success" style="margin-left: 10px;">添加</Button>
            </div>
        </div>
        <br>
        <Table :columns="columns" :data="list" border stripe size="small">
            <template slot-scope="{ row , index}" slot="makeSlot">
                <Button type="info" @click="edit(row, false)" size="small" style="margin-left: 1px;">编辑</Button>
                <Button type="error" @click="del(row.id)" style="margin-left: 1px;" size="small">删除</Button>
            </template>
        </Table>
        <br>

        <Paginate :total="total" @on-change="changePage" @on-page-size-change="changeSize" :current="searchData.page">
        </Paginate>


        <Modal title="城市信息" width="40" v-model="showAdd" @on-ok="editModalOk" :footer-hide="formItem.is_detail">
            <Form ref="formValidate" :model="formItem" :label-width="80">
                <FormItem label="城市">
                    <Select v-model="formItem.city_id" filterable>
                        <Option :value="val.id" v-for="val in cityList" :key="val.id">{{ val.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label="排序">
                    <Input v-model="formItem.sort" type="number"></Input>
                </FormItem>
            </Form>
        </Modal>
    </div>
</template>

<script>
import Paginate from "_c/paginate";
export default {
    name: "city",
    components: {
        Paginate
    },
    data() {
        return {
            searchData: {
                page: 1,
                limit: 10,
                order_by: 'sort',
            },
            // 列表
            columns: [
                {
                    title: '名称',
                    key: 'city_name',
                    align: "center"
                },
                {
                    title: '排序',
                    key: 'sort',
                    align: "center"
                },
                {
                    title: '创建时间',
                    key: 'create_time',
                    align: "center"
                },
                {
                    title: '操作',
                    slot: 'makeSlot',
                },
            ],
            // 数据
            list: [],
            total: 0,
            formItem: {},
            showAdd: false,
            cityList: [],
        }
    },
    methods: {
        // 查询管理员信息
        getList() {
            this.$axios('/admin/partner/city/getList', this.searchData).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        reset() {
            this.searchData = {
                page: 1,
                limit: 10,
                order_by: 'sort',
            };
            this.getList();
        },
        changePage(page) {
            this.searchData.page = page;
            this.getList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getList();
        },
        customSort(column, key, order) {
            this.searchData.order_by = column.key;
            this.searchData.sort = column.order;
            this.getList();
        },
        editModalOk() {
            this.$axios('/admin/partner/city/save', this.formItem).then(res => {
                this.$Message.success('添加成功');
                this.showAdd = false;
                this.formItem = {};
                this.getList();
            });
        },
        edit(row, isDetail) {
            if (row) {
                this.formItem = { ...row };
            } else {
                this.formItem = {
                    id: 0,
                    name: "1",
                }
            }
            this.showAdd = true;
        },
        del(id) {
            this.$Modal.confirm({
                title: '删除',
                content: "<p>您确定要删除吗？</p>",
                onOk: () => {
                    this.$axios('/admin/partner/city/del', { id: id }).then(res => {
                        this.$Message.success('删除成功');
                        this.getList();
                    });
                },
                onCancel: () => {
                    this.$Message.info('取消操作');
                }
            });
        },
        getCityList() {
            this.$axios("/admin/config/getCityList").then(res => {
                this.cityList = res;
            });
        },
    },
    created() {
        this.getCityList();
        this.getList();
    }
}
</script>
